package cv.gzd.robot.controller;

import cv.gzd.common.utils.AjaxMsg;
import cv.gzd.common.utils.Page;
import cv.gzd.robot.pojo.Authorization;
import cv.gzd.robot.pojo.User;
import cv.gzd.robot.pojo.vo.ResultVo;
import cv.gzd.robot.pojo.vo.UserQueryVo;
import cv.gzd.robot.service.AuthorizationService;
import cv.gzd.robot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import javax.xml.transform.Result;
import java.util.List;

/**用户控制器
 * @author Jun
 */
@Controller
public class UserController {
    @Autowired
    private AuthorizationService authorizationService;
    @Autowired
    private UserService userService;


    /**
     * 登录实例
     *
     * @return
     */
    @RequestMapping(value = "/login.action", method = RequestMethod.GET)
    public String login() {
        return "login";
    }

    @RequestMapping(value = "/login.action", method = RequestMethod.POST)
    @ResponseBody
    public ResultVo login(String username, String password, HttpSession httpSession) {
        ResultVo resultVo=new ResultVo();
        if (username != null && username!="") {
            if (password!=null&&password!=""){
                User user = userService.selectUserByUserNameAndPassword(username, password);
                if (user != null) {
                    httpSession.setAttribute("USER_SESSION", user);
                    resultVo.setCode(AjaxMsg.LOGIN_SUCCESS.getCode());
                    resultVo.setMsg("登录成功");
                    return resultVo;
                }else {
                    resultVo.setCode(AjaxMsg.LOGIN_FAIL.getCode());
                    resultVo.setMsg("用户名或密码错误");
                    return resultVo;
                }
            }else {
                resultVo.setCode(AjaxMsg.LOGIN_FAIL2.getCode());
                resultVo.setMsg("用户或密码不能为空");
                return resultVo;
            }
        }
            resultVo.setCode(AjaxMsg.LOGIN_FAIL2.getCode());
            resultVo.setMsg("用户或密码不能为空");
            return resultVo;

    }

    /**
     * 登出实例
     * @param httpSession
     * @return
     */
    @RequestMapping("/logout")
    public String logout(HttpSession httpSession){
        httpSession.removeAttribute("USER_SESSION");
        httpSession.removeAttribute("USER_ID_SESSION");
        return "login";
    }

    /**
     * 显示用户列表
     * @param model 返回模型
     * @param userVo user查询类
     * @return
     */
    @RequestMapping("/user/list")
    public String userList(Model model, UserQueryVo userVo,HttpSession session){
        List<Authorization> TypAuthorization=authorizationService.selectAuthorizationByCode("001");
        model.addAttribute("Typeauthorization",TypAuthorization);
        User user= (User) session.getAttribute("USER_SESSION");
        if(!user.getAuthorization().equals("超级管理员")){
            userVo.setUserization("管理员");
        }
        Page<User> page= userService.selectPageByUserQueryVo(userVo);
        model.addAttribute("page",page);
        model.addAttribute("username",userVo.getUsername());
        model.addAttribute("authorization",userVo.getAuthorization());
        model.addAttribute("realname", userVo.getRealname());

        return "user";
    }

    /**
     * 修改用户密码
     * @param uid
     * @param password
     * @param pwsd
     * @return
     */
    @RequestMapping("/user/updatepswd.action")
    public @ResponseBody
    ResultVo updatepswd(Integer uid,String password,String pwsd){
        ResultVo resultVo = new ResultVo();

        if(password.equals(pwsd)){
            User user=new User();
            user.setUid(uid);
            user.setPassword(password);
            userService.updateUser(user);
            resultVo.setCode(AjaxMsg.SUCCESS.getCode());
            return resultVo;
        }
        resultVo.setCode(AjaxMsg.FAIL.getCode());
        return resultVo;
    }


    /**
     * 修改用户信息界面参数回调
     * @param id
     * @return
     */
    @RequestMapping("/user/edit.action")
    public @ResponseBody
    User edit(Integer id){
        return userService.selectUserByUid(id);
}

    /**
     * 保存修改用户信息
     * @param user
     * @return
     */
    @RequestMapping("/user/update.action")
    public  @ResponseBody
    ResultVo updateUser(User user){
        ResultVo resultVo=new ResultVo();
        userService.updateUser(user);
        resultVo.setCode(AjaxMsg.SUCCESS.getCode());
        return resultVo;
    }

    /**
     * 删除用户信息
     * @param id
     * @return
     */
    @RequestMapping("/user/delete.action")
    public @ResponseBody
    ResultVo deleteUser(Integer id){
        userService.deleteUser(id);
        ResultVo resultVo=new ResultVo();
        resultVo.setCode(AjaxMsg.SUCCESS.getCode());
        return resultVo;
    }

    /**
     * 保存新增用户信息
     * @param user
     * @return
     */
    @RequestMapping("/user/save.action")
    public @ResponseBody
    ResultVo saveUser(User user){
        ResultVo resultVo=new ResultVo();
        String username=user.getUsername();
        if(userService.selectUserByUserName(username)==null){
            userService.saveUser(user);
            resultVo.setCode(AjaxMsg.SUCCESS.getCode());
            return resultVo;
        }
        resultVo.setCode(AjaxMsg.FAIL.getCode());
        return resultVo;
    }

}
